package com.ymatou.iapi.trading.sellerquery2.parameter.sp;

import java.util.List;
import java.util.Map;

import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;

/**
 * 对应存储过程：sp_trd_GetSellerOrdersListSummaryV2
 * 
 * @author songyefeng
 *
 */
public class Sp_GetSellerOrdersListSummaryV2 {
	private YmtOrdersWapper ymtOrdersWapper = new YmtOrdersWapper();

	private int sellerId;
	private Integer orderSellerAcceptedStatus;
	private List<Integer> orderStatus;
	private Boolean paidInFull;
	private String timeFrom;
	private String timeTo;
	private Boolean considerOrderStatus;
	private Boolean considerRCOrderEstablish;
	private Boolean considerRCAccountPaid;
	private Boolean considerRestOrderStatus;
	private int filter;
	private Object filterValue;

	public int getSellerId() {
		return sellerId;
	}

	public void setSellerId(int sellerId) {
		this.sellerId = sellerId;
	}

	public Integer getOrderSellerAcceptedStatus() {
		return orderSellerAcceptedStatus;
	}

	public void setOrderSellerAcceptedStatus(Integer orderSellerAcceptedStatus) {
		this.orderSellerAcceptedStatus = orderSellerAcceptedStatus;
	}

	public List<Integer> getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(List<Integer> orderStatus) {
		this.orderStatus = orderStatus;
	}

	public Boolean getPaidInFull() {
		return paidInFull;
	}

	public void setPaidInFull(Boolean paidInFull) {
		this.paidInFull = paidInFull;
	}

	public String getTimeFrom() {
		return timeFrom;
	}

	public void setTimeFrom(String timeFrom) {
		this.timeFrom = timeFrom;
	}

	public String getTimeTo() {
		return timeTo;
	}

	public void setTimeTo(String timeTo) {
		this.timeTo = timeTo;
	}

	public Boolean getConsiderOrderStatus() {
		return considerOrderStatus;
	}

	public void setConsiderOrderStatus(Boolean considerOrderStatus) {
		this.considerOrderStatus = considerOrderStatus;
	}

	public Boolean getConsiderRCOrderEstablish() {
		return considerRCOrderEstablish;
	}

	public void setConsiderRCOrderEstablish(Boolean considerRCOrderEstablish) {
		this.considerRCOrderEstablish = considerRCOrderEstablish;
	}

	public Boolean getConsiderRCAccountPaid() {
		return considerRCAccountPaid;
	}

	public void setConsiderRCAccountPaid(Boolean considerRCAccountPaid) {
		this.considerRCAccountPaid = considerRCAccountPaid;
	}

	public Boolean getConsiderRestOrderStatus() {
		return considerRestOrderStatus;
	}

	public void setConsiderRestOrderStatus(Boolean considerRestOrderStatus) {
		this.considerRestOrderStatus = considerRestOrderStatus;
	}

	public Integer getFilter() {
		return filter;
	}

	public void setFilter(Integer filter) {
		this.filter = filter;
	}

	public Object getFilterValue() {
		return filterValue;
	}

	public void setFilterValue(Object filterValue) {
		this.filterValue = filterValue;
	}

	@SuppressWarnings("rawtypes")
	public List<Map> execute() {
		String queryField = "count(o.iOrderId) OrdersCount,"
				+ "sum(o.fOrderPrice + o.fFreight + o.fOrderDiscount - isnull(o.fUseGiftAmount, 0) - isnull(o.fYmtCouponAmount, 0) -"
				+ "isnull(o.fSellerCouponAmount, 0) - isnull(o.[fSellerPromotionAmount], 0) +"
				+ "isnull( p.fAmount, o.fTotalPrice - o.fOrderPrice))  ExpectedTotalPrice, "
				+ "sum(o.fOrderPrice + o.fFreight + o.fOrderDiscount - isnull(o.fUseGiftAmount, 0) - "
				+ "isnull(o.fSellerCouponAmount, 0) - isnull(o.[fSellerPromotionAmount], 0) +"
				+ "isnull( p.fAmount, o.fTotalPrice - o.fOrderPrice)-isnull(s.fRefundedAmountOfCash, 0)-isnull(s.fRefundedAmountOfCoupon, 0))  ExpectedTotalPriceYMTCoupon, "
				/**
				 * 2016-11-07
				 * 增加条件 iff(s.fPaidAmountOfCash<0.01
				 */
				+ "sum(iif((s.fPaidAmountOfCash<0.01 or o.iTradingStatus in (12,13,18)),0,o.fTotalPrice + o.fFreight))";

		String joinCondition = " with (nolock,index = IX_Ymt_Orders)  "
				//砍价团订单，在组团成功前，结果不展示。add by sean 03.01
				+ " LEFT JOIN dbo.Ymt_OrderBiz b ON b.OrderId = o.iOrderId inner loop join Ymt_OrderState s with (nolock,index = PK_Ymt_OrderState)  on o.iOrderId = s.iOrderId"
				+ " outer apply (select top 1 fAmount,fUseGiftAmount from Ymt_OrderPostPay with (nolock,index = IX_Ymt_OrderPostPay) where iOrderId = o.iOrderId and (iAction = 0 or iAction = 1) order by iAction desc) p";
		StringBuffer queryCondition = new StringBuffer();
		queryCondition
				.append("( b.OrderId IS NULL  OR b.BizStatus =3) and o.iBuyerId = "
						+ sellerId);
		if (false == DataUtil.isNullOrEmpty(orderStatus)) {
			queryCondition.append(" and o.iTradingStatus in "
					+ DataUtil.toInOfSql2(orderStatus));
		}
		if (timeFrom != null) {
			queryCondition.append(" and o.dAddTime >= '" + timeFrom + "'");
		}
		if (timeTo != null) {
			queryCondition.append(" and o.dAddTime < '" + timeTo + "'");
		}
		if (filter == 1) {
			queryCondition.append(" and o.iOrderId = '" + filterValue + "'");
		}
		if (filter == 2) {
			queryCondition.append(" and o.sBuyerLoginId = '" + filterValue
					+ "'");
		}
		if (filter == 3) {
			queryCondition.append(" and o.sReceivePerson = '" + filterValue
					+ "'");
		}
		if (filter == 4) {
			//			queryCondition
			//					.append(" and exists(select top 1 1 from Ymt_OrderSummary(nolock) where iOrderId = o.iOrderId and sSummary = '"
			//							+ filterValue + "')");
			//			queryCondition.append(" and exists(SELECT  top 1 1 FROM dbo.Ymt_BillCodeSummaryToOrders bco JOIN dbo.Ymt_BillCodeSummary bc ON bco.BillCodeSummaryID = bc.billCodeSummaryID WHERE bc.IsDeleted = 0  and bco.OrderID = o.iOrderId and  bc.BillCode = '"+filterValue+"')");
			queryCondition
					.append(" and exists(SELECT  top 1 1 FROM dbo.Ymt_BillCodeSummaryToOrders bco JOIN dbo.Ymt_BillCodeSummary bc ON bco.BillCodeSummaryID = bc.billCodeSummaryID WHERE bc.IsDeleted = 0  and bco.OrderID = o.iOrderId and  bc.BillCode like '%"
							+ filterValue + "%')");
		}
		return ymtOrdersWapper.selectOrders2(queryField, joinCondition,
				queryCondition.toString());
	}
}
